﻿<%@ Page Language="C#" AutoEventWireup="true" Inherits="ZX.Web.UI.Controls.DataGrid" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="..//SFramework/boot.js"></script>


    <style type="text/css">
        body {
            padding-left: 0px;
            font-size: 13px;
        }

        h1 {
            font-size: 20px;
            font-family: Verdana;
        }

        h4 {
            font-size: 16px;
            margin-top: 25px;
            margin-bottom: 10px;
        }

        .description {
            padding-bottom: 30px;
            font-family: Verdana;
        }

            .description h3 {
                color: #CC0000;
                font-size: 16px;
                margin: 0 30px 10px 0px;
                padding: 45px 0 8px;
                /*background:url(titleback.png) no-repeat scroll left bottom transparent*/
                border-bottom: solid 1px #888;
            }

        body {
            margin: 0;
            padding: 0;
            border: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>

    <!--引入皮肤样式-->


</head>

<body>
    <div class="mini-splitter" style="width: 100%; height: 100%;">
        <div size="240" showcollapsebutton="true">
            <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
                <a class="mini-button" iconcls="icon-add" plain="true" onclick="fnAddRole()">新增</a>
                <a class="mini-button" iconcls="icon-remove" plain="true" onclick="fnEditRole()">修改</a>
            </div>
            <div class="mini-fit">
                <ul id="tree1" class="mini-tree" url="/extendpage/data/jsondata.aspx?entity=v_data_role_group&type=1" style="width: 100%;"
                    showtreeicon="true" textfield="name" idfield="id" parentfield="parent_id" resultastree="false">
                </ul>
            </div>
        </div>
        <div showcollapsebutton="true">
            <div id="tabs1" class="mini-tabs" activeindex="0" style="width: 100%; height: 100%; padding: 0px; margin: 0px" plain="false">
                <div title="拥有权限" style="padding: 0px; margin: -5px;">
                    <div class="mini-toolbar" style="padding: 0px; border-bottom: 0;">
                        <a class="mini-button" iconcls="icon-add" plain="true" onclick="fnSetFunction()">设置功能权限</a>
                        <a class="mini-button" iconcls="icon-save" plain="true" onclick="fnTreeGridSave()">保存</a>
                    </div>
                    <div class="mini-fit">
                        <div id="treegrid1" class="mini-treegrid" style="width: 100%; height: 98%;"
                            url="/extendpage/data/jsondata.aspx?entity=v_data_role_function&condition=1=0&type=1" showtreeicon="true"
                            treecolumn="name" idfield="f_id" parentfield="parent_id" resultastree="false" allowcelledit="true" allowcellselect="true"
                            allowresize="true" expandonload="true">
                            <div property="columns">
                                <div type="indexcolumn"></div>
                                <div name="name" field="name" width="160">菜单名称</div>
                                <div field="bar_items" name="bar_items" width="120" headeralign="left">
                                    操作权限               
                 <input property="editor" class="mini-buttonedit" onbuttonclick="onButtonEdit" style="width: 100%;" />
                                </div>
                                <div field="operate_range_type" width="100" headeralign="center" allowsort="true" type="comboboxcolumn">数据范围<input property="editor" class="mini-combobox" style="width: 100%;" data="m_op_data" /></div>
                                <div field="entity_name" width="250" headeralign="center" allowsort="true">部门<input property="editor" class="mini-textbox" style="width: 100%;" /></div>
                                <div field="page" width="250" headeralign="center" allowsort="true">自定义条件<input property="editor" class="mini-textarea" style="width: 100%;" /></div>
                            </div>

                        </div>
                    </div>
                </div>
                <div title="包含用户" style="padding: 0px; margin: -5px">
                    <div class="mini-toolbar" style="padding: 0px; border-bottom: 0;">
                        <a class="mini-button" iconcls="icon-add" plain="true" onclick="fnSetUser()">设置用户</a>
                        <a class="mini-button" iconcls="icon-remove" plain="true" onclick="fnRemoveUser()">移除用户</a>
                    </div>
                    <div class="mini-fit">
                        <div id="grid1" class="mini-datagrid" style="width: 100%; height: 100%;"
                            allowcelledit="true"
                            url="/extendpage/data/jsondata.aspx?entity=t_sys_user&type=1"
                            allowcellselect="true">
                            <div property="columns">
                                <%=DataFields %>
                            </div>
                        </div>
                    </div>
                </div>
                <div title="包含桌面" style="padding: 0px; margin: -5px">
                    <div class="mini-toolbar" style="padding: 0px; border-bottom: 0;">
                        <a class="mini-button" iconcls="icon-add" plain="true" onclick="fnSetDesktop()">设置桌面</a>
                        <a class="mini-button" iconcls="icon-remove" plain="true" onclick="fnRemoveDesktop()">移除桌面</a>
                    </div>
                    <div class="mini-fit">
                        <div id="desktopgrid" class="mini-datagrid" style="width: 100%; height: 100%;"
                            allowcelledit="true"
                            url="/extendpage/data/jsondata.aspx?entity=t_set_desktop&type=1"
                            allowcellselect="true">
                            <div property="columns">
                                <div field="name" width="50" headeralign="center" allowsort="true" renderer="">桌面项名称</div>
                                <div field="title" width="100" headeralign="center" allowsort="true" renderer="">显示标题</div>
                                <div field="data_url" width="100" headeralign="center" allowsort="true" renderer="">数据URL</div>
                                <div field="area" width="50" headeralign="center" allowsort="true" renderer="">区域(R（右）/L（左))</div>
                                <div field="content_type" width="50" headeralign="center" allowsort="true" renderer="">内容类型(0,内容ajax加载,1,iframe 页面加载)</div>
                                <div field="sort" width="50" headeralign="center" allowsort="true" renderer="">排序</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        var m_op_data=[{"id":"0","text":"全部"},{"id":"1","text":"本人"},{"id":"2","text":"本部门"},{"id":"4","text":"本公司"},{"id":"5","text":"本事业部"},{"id":"6","text":"自定义部门"},{"id":"7","text":"自定义条件"}];
        var _openWindowHeight=<%=EditWindowHeight%>;
        var _openWindowWidth=<%=EditWindowWidth%>;
        var const_EditEntity="<%=EditEntity%>";
        <% =ColumnDataSource%>
        var const_EditEntity = "<%=EditEntity%>";
    </script>
    <script>
        <% =Edit_self_script %>
    </script>
    <script type="text/javascript">
        mini.parse();

        var tree = mini.get("tree1");
        var grid = mini.get("grid1");
        var treeGrid =  mini.get("treegrid1");
        var desktopGrid = mini.get("desktopgrid");
        //var panelGrid = mini.get("panelGrid1");
        var current_node = null;
        tree.on("nodeselect", function (e) {
            current_node = e.node;
            //debugger
            if (e.isLeaf) {
                grid.load({ condition : "<%=GetStringFromQueryString("fkey","id")%> in (select user_id from t_Sys_User_role where role_id='"+e.node.id+"')" });
                desktopGrid.load({ condition : "id in (select desktop_id from t_Sys_role_desktop where role_id='"+e.node.id+"')" });
                treeGrid.load("/extendpage/data/jsondata.aspx?entity=v_data_role_function&condition=role_id='"+e.node.id+"'&type=1");
           
            } else {
                grid.setData([]);
                grid.setTotalCount(0);
                desktopGrid.setData([]);
                desktopGrid.setTotalCount(0);

            }
        });
  

        function addRow() {
            var node = tree.getSelectedNode();
            if (node) {
                var newRow = { "id":"","<%=GetStringFromQueryString("fkey","parent_id")%>":node.id,"name": "" };
                //newRow.parent_id = node.id;
                grid.addRow(newRow, 0);
            }
        }

        function removeRow() {
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                grid.removeRows(rows, true);
            }
        }

        var fnAddRole= function(){
            var node = tree.getSelectedNode();
            mini.open({
                url: "/ExtendPage/editview/DefineEditForm.aspx?entity=t_sys_role&id=",
                title: "新增角色", width: _openWindowWidth, height: _openWindowHeight,
                onload: function () {
                    //var iframe = this.getIFrameEl();
                    //var data = { action: "new" };
                    //iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    tree.reload();
                }});
        }   


        function fnSetFunction() {
            var node = tree.getSelectedNode();
            mini.open({
                url: "/extendpage/template/dialogpage/TreeWindow.html?roleid="+node.id,    
                title: "菜单选择",
                width: 350,
                height: 350,
                ondestroy: function (action) {
                  
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        
                        var data = iframe.contentWindow.GetData();
                        //alert(data);
                        var _data="";
                        //debugger
                        $.each(data,function(e){
                            _data=_data +"@"+data[e].id;
                           
                        });
                        fnRunProc("[usp_set_role_function]",node.id+","+_data);
                        treeGrid.load();
                    }
                }
            });            
             
        }  

        //
        var fnAdd = function () {
            mini.open({
                url: "/ExtendPage/editview/DefineEditForm.aspx?entity=<%=EditEntity%>&id=",
                title: "新增", width: _openWindowWidth, height: _openWindowHeight,
                onload: function () {
                    //var iframe = this.getIFrameEl();
                    //var data = { action: "new" };
                    //iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    grid.reload();
                }
            });
        }

            //
            var fnEdit = function () {
                var row = grid.getSelected();
                if (row) {
                    mini.open({
                        url: "/ExtendPage/editview/DefineEditForm.aspx?entity=<%=EditEntity%>&id=" + row.id,
                        title: "编辑", width: _openWindowWidth, height: _openWindowHeight,
                        onload: function () {
                            //var iframe = this.getIFrameEl();
                            //var data = { action: "edit", id: row.id };
                            //iframe.contentWindow.SetData(data);
                        },
                        ondestroy: function (action) {
                            grid.reload();

                        }
                    });

            } else {
                alert("请选中一条记录");
            }
            }



        var fnGridSave = function () {
            //debugger
            var data = grid.getChanges();
            var json = mini.encode(data);
            //alert(json);
            grid.loading("保存中，请稍后......");
            $.ajax({
                url: "/extendpage/action/datasave.aspx?entity=<%=EditEntity%>&action=batch&flag=1",
                data: { data: json },
                type: "post",
                success: function (text) {

                    alert("批量保存成功!");
                    grid.reload();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });
        }

        ///权限的数据保存
        var fnTreeGridSave = function () {
            //debugger
            var data =treeGrid.getChanges();
            var json = mini.encode(data);
            //alert(json);
            grid.loading("保存中，请稍后......");
            $.ajax({
                url: "/extendpage/action/datasave.aspx?entity=t_sys_role_function&action=batch&flag=1",
                data: { data: json },
                type: "post",
                success: function (text) {
                    grid.reload();
                    alert("批量保存成功!");
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });
        }
       

        var fnAddMaster = function () {
            if(typeof(fnCustomAddMaster)!="undefined"){
                fnCustomAddMaster();
                return;
            }
            fnInitData();
        }
        function fnInitData() {
            mini.prompt("表名或视图名：", "请输入",
                    function (action, value) {
                        if (action == "ok") {
                            fnRunProc2("usp_init_datagrid_setting",value+","+value);
                        } else {
                            alert("取消");
                        }

                    }
                );
        }


        var win = null;
        var _e = null;
        function onButtonEdit(e) {
            _e = e.sender;
            var buttonEdit = e.sender;
            var row = treeGrid.getSelected();
            fnSelectData("/extendpage/datagrid/selectgrid.aspx?entity=t_sys_function_operation&order=sort&desc=Yes&condition=is_common='Y' or function_id='"+row.f_id+"'",fnSetItems);
        }

        var fnSetItems=function(data){
            //var _treeGrid =mini.get("treegrid1");
            //var _row = treeGrid.getSelected();
            var a = [];
            $.each(data,function(i){a.push(data[i].name);})
            var _a = a.join(',');
            //_treeGrid.updateRow(_row, {
            //    bar_items:_a
            //});
            if(_e!=null){
                _e.setValue(_a);
                _e.setText(_a);
            }
        }
        var  fnSelectData = function(url,callback){
            var node = tree.getSelectedNode();
            mini.open({
                url: url,
                title: "选择列表",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    //if (action == "close") return false;
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.fnGetData();
                        // debugger
                        data = mini.clone(data);    //必须
                        callback(data);
                    }
                },
                onload: function () {   
                    var row = treeGrid.getSelected();
                    //弹出页面加载完成
                    var iframe = this.getIFrameEl(); 
                    var rows = fnGetData("t_sys_function_operation","(is_common='Y' or function_id='"+row.function_id+"') and '"+row.bar_items+"' like '%'+name+'%'");
                    //alert(rows.length)
                    var data = rows.data;       
                    //调用弹出页面方法进行初始化
                    iframe.contentWindow.fnSetData(data); 
                        
                },
            });  
        }
        //设置用户
        function fnSetUser() {
            var node = tree.getSelectedNode();
            mini.open({
                url: "/extendpage/datagrid/selectgrid.aspx?entity=t_sys_user&condition=id not in (select user_id from t_sys_user_role where role_id='"+node.id+"')",
                title: "选择列表",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    //if (action == "close") return false;
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.fnGetData();
                        data = mini.clone(data);    //必须
                        //alert(data);
                        var _data="";
                        //debugger
                        $.each(data,function(e){
                            _data=_data +"@"+data[e].id;
                        });
                        //alert(_data)
                        fnRunProc("[usp_set_role_users]",node.id+","+_data);
                        grid.load();
                    }

                }
            });  


        }
        //移除用户
        var fnRemoveUser = function () {
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                if (confirm("确定删除选中记录？")) {
                    var ids = [];
                    for (var i = 0, l = rows.length; i < l; i++) {
                        var r = rows[i];
                        ids.push(r.id);
                    }
                    var id = ids[0]; //ids.join(',');
                    grid.loading("操作中，请稍后......");
                    $.ajax({
                        url: "/extendpage/data/jsondata.aspx?type=1&entity=t_sys_user_role&condition=user_id='" + id+"' and role_id='"+current_node.id+"'" ,
                        success: function (text) {
                            //依赖于jquery.json.2.2.js
                            var c =  $.evalJSON(text);
                            $.ajax({
                                url: "/extendpage/action/datasave.aspx?entity=t_sys_user_role&action=delete&id=" + c[0].id+"&r="+Math.random(),
                                success: function (text) {
                                    if (text == 1) {
                                        alert("删除成功!");
                                    }
                                    grid.reload();
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    alert(jqXHR.responseText);
                                    grid.reload();
                                }
                            });
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                            grid.reload();
                        }
                    });
                  
                }
            } else {
                alert("请选中一条记录");
            }
        }

        var fnSetDesktop=function(){
            var node = tree.getSelectedNode();
            mini.open({
                url: "/extendpage/datagrid/selectgrid.aspx?entity=t_set_desktop&condition=id not in (select desktop_id from t_sys_role_desktop where role_id='"+node.id+"')",
                title: "选择列表",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    //if (action == "close") return false;
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.fnGetData();
                        data = mini.clone(data);    //必须
                        //alert(data);
                        var _data="";
                        //debugger
                        $.each(data,function(e){
                            _data=_data +"@"+data[e].id;
                        });
                        //alert(_data)
                        fnRunProc("[usp_set_role_desktops]",node.id+","+_data);
                        desktopGrid.load();
                    }

                }
            });  
        }
        
        var fnRemoveDesktop = function(){
            var rows = desktopGrid.getSelecteds();
            if (rows.length > 0) {
                if (confirm("确定删除选中记录？")) {
                    var ids = [];
                    for (var i = 0, l = rows.length; i < l; i++) {
                        var r = rows[i];
                        ids.push(r.id);
                    }
                    var id = ids[0]; //ids.join(',');
                    grid.loading("操作中，请稍后......");
                    $.ajax({
                        url: "/extendpage/data/jsondata.aspx?type=1&entity=t_sys_role_desktop&condition=desktop='" + id+"' and role_id='"+current_node.id+"'" ,
                        success: function (text) {
                            //依赖于jquery.json.2.2.js
                            var c =  $.evalJSON(text);
                            $.ajax({
                                url: "/extendpage/action/datasave.aspx?entity=t_sys_role_desktop&action=delete&id=" + c[0].id+"&r="+Math.random(),
                                success: function (text) {
                                    if (text == 1) {
                                        alert("删除成功!");
                                    }
                                    grid.reload();
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    alert(jqXHR.responseText);
                                    grid.reload();
                                }
                            });
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                            grid.reload();
                        }
                    });
                  
                }
            } else {
                alert("请选中一条记录");
            }}

       
    </script>
    <script src="/Scripts/common/jquery.action.js"></script>
    <script src="/Scripts/common/jquery.data.js"></script>
    <script src="/Scripts/datagrid/UserSelectWindow.js"></script>
    <script src="..//SFramework/jquery.json-2.2.js"></script>
</body>

</html>
